/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.zfdxk.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.hcht.xrlh.sx.entity.DrSx;
import com.hcht.xrlh.sx.service.DrSxService;
import com.hcht.xrlh.zfdxk.entity.DrZfdxk;
import com.hcht.xrlh.zfdxk.service.DrZfdxkService;
import com.hcht.xrlh.zfdxktitle.entity.DrZfdxkTitle;
import com.hcht.xrlh.zfdxktitle.service.DrZfdxkTitleService;

/**
 * 执法对象Controller
 * @author lrz
 * @version 2016-12-20
 */
@Controller
@RequestMapping(value = "${adminPath}/zfdxk/drZfdxk")
public class DrZfdxkController extends BaseController {

	@Autowired
	private DrZfdxkService drZfdxkService;
	@Autowired
	private DrSxService sxService;
	@Autowired
	private DrZfdxkTitleService drZfdxkTitleService;
	
	@ModelAttribute
	public DrZfdxk get(@RequestParam(required=false) String id) {
		DrZfdxk entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drZfdxkService.get(id);
		}
		if (entity == null){
			entity = new DrZfdxk();
		}
		return entity;
	}
	
	@RequiresPermissions("zfdxk:drZfdxk:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrZfdxk drZfdxk, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrZfdxk> page = drZfdxkService.findPage(new Page<DrZfdxk>(request, response), drZfdxk); 
		model.addAttribute("page", page);
		return "xrlh/zfdxk/drZfdxkList";
	}

	@RequiresPermissions("zfdxk:drZfdxk:view")
	@RequestMapping(value = "form")
	public String form(DrSx drSx, Model model,HttpServletRequest request, HttpServletResponse response) {
		drSx = sxService.get(request.getParameter("id"));
		List<DrZfdxk> list_zfdx = Lists.newArrayList();
		DrZfdxk drZfdxk = new DrZfdxk();
		drZfdxk.setDrSx(drSx);
		list_zfdx = drZfdxkService.findList(drZfdxk);	
		drSx.setDrZfdxkList(list_zfdx);
		
		List<DrZfdxkTitle> list_title = Lists.newArrayList();
		DrZfdxkTitle title = new DrZfdxkTitle();
		title.setSxId(drSx.getId());
		list_title = drZfdxkTitleService.findList(title);
		model.addAttribute("drSx", drSx);
		model.addAttribute("list_title", list_title);
		return "xrlh/zfdxk/drZfdxkForm";
	}

	@RequiresPermissions("zfdxk:drZfdxk:edit")
	@RequestMapping(value = "save")
	public String save(DrSx drSx, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		if (!beanValidator(model, drSx)){
			return form(drSx, model,request,response);
		}
		drZfdxkService.save(drSx);
		addMessage(redirectAttributes, "保存执法对象成功");
		return "redirect:"+Global.getAdminPath()+"/sx/drSx/?repage";
	}
	
	@RequiresPermissions("zfdxk:drZfdxk:edit")
	@RequestMapping(value = "delete")
	public String delete(DrZfdxk drZfdxk, RedirectAttributes redirectAttributes) {
		drZfdxkService.delete(drZfdxk);
		addMessage(redirectAttributes, "删除执法对象成功");
		return "redirect:"+Global.getAdminPath()+"/zfdxk/drZfdxk/?repage";
	}

}